<?
session_start();
$_SESSION['lastPage'] = "Photos";
$searchTrue = false;
$searchFalse = false;
$noResults = false;

require("../mysql.config.inc");

$mysqli = new mysqli($host,$username,$password,$db);

if(isset($_POST['searchString']) && preg_match("|^[a-zA-Z0-9.,'\s]+$|", $_POST['searchString'])){
	$searchString = $_POST['searchString'];
	$searchTrue = true;
}elseif(isset($_POST['searchString']) && !preg_match("|^[a-zA-Z0-9.,'\s]+$|", $_POST['searchString'])){
	$searchFalse = true;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>A Photo Gallery</title>
	<link rel="stylesheet" type="text/css" href="stylesheets/styles.css"/>
</head>

<body>
<div id="content">
	
	
	<div id="nav">
		<dl class="menu">
			<dd><a href="index.php">Home</a></dd>
			<dd><a href="albums.php">Albums</a></dd>
			<dd class = "active"><a href="photos.php">All Photos</a></dd>
			<dd><a href="admin.php">Administration</a></dd>
		</dl>
	</div>
	
	<h2>All Photos</h2>
	
	<div id="bodyText">
		These are all the photos that are currently in our collection. 
		<form action="photos.php" method="post">
			<p>
			Search for photos by caption: <input type="text" name="searchString"/>
			<input type="submit" value="Search"/>
			<?if($searchFalse){
				print"<br/><span class=\"alert\">You may only put in a search string with letters or numbers.</span>";
			}?>
			</p>
		</form>
	</div>
	
	<br/><br/>
	
	<div id="allPhotos">
	<?
		if($searchTrue){
			$result = $mysqli->query("SELECT DISTINCT pid, Caption, Link FROM Photos NATURAL JOIN AlbumIn WHERE Caption LIKE '%$searchString%' ORDER BY pid");
			$numRows = $result->num_rows;
			if($numRows > 0){
				while($photos = $result->fetch_assoc()){
					print("<a href=\"http://info230.cs.cornell.edu/users/aballantine/www/p3/viewphoto.php?pid=".$photos['pid']."\" class=\"thumb\">");
					print("<img src=\"thumbs/".$photos['Link']."\" alt=\"Photo Gallery\" title=\"".$photos['Caption']."\" /></a>\n");
				}	 
			}else{
				print "There were no results for your search. Try again.";
			}
		}else{
			$result = $mysqli->query("SELECT DISTINCT pid, Caption, Link FROM Photos NATURAL JOIN AlbumIn ORDER BY pid");
			while($photos = $result->fetch_assoc()){
				print("<a href=\"http://info230.cs.cornell.edu/users/aballantine/www/p3/viewphoto.php?pid=".$photos['pid']."\" class=\"thumb\">");
				print("<img src=\"thumbs/".$photos['Link']."\" alt=\"Photo Gallery\" title=\"".$photos['Caption']."\" /></a>\n");
			}
		}
		$mysqli->close();
	?>
	</div>
</div>
</body>
</html>